/**
 * Created by 谢五羊 on 2016/12/8 0008.
 */
$(function () {
    layui.use('layer', function(){
        var layer = layui.layer;
    });
    $('.xwy-add').click(function () {
        var w = $(this).attr('data-width')?$(this).attr('data-width'):'800px';
        var h = $(this).attr('data-height')?$(this).attr('data-height'):'400px';
        var url = $(this).attr('data-url');console.log(url);
        if(!url){
            layer.msg('路径都没有，打开个毛线？', {
                offset: 't',
                anim: 6
            });
        }else{
            var index = layer.open({
                type: 2,
                content: url,
                area: [w, h], //宽高
                maxmin: true
            });
        }
        layer.full(index);
    });
    $('.ajax-post').click(function () {
        _this = $(this);
        var target,fdata,form,_this,target_form,isFlag = false;
        target_form = _this.attr('target-form');
        form = $('#'+target_form);
        if(form.get(0) == undefined){
            layer.msg('没找到表单啊，老铁', {
                offset: 't',
                anim: 6
            });
            return false;
        }else if(form.get(0).nodeName == 'FORM'){
            //判断form是不是表单
            var fcheck = form.find(".verif_null");
            fcheck.each(function () {
                if($(this).hasClass("verif_null")){
                    var s = isnull($(this));
                    if(!s){
                        isFlag = true;
                        return false;
                    }
                }
            });
            if(isFlag){
                return false;
            }
            var target_msg = _this.attr('target-msg');
            if(target_msg !== undefined){
                //判断提交前的再次确定信息
                layer.confirm(target_msg, {
                    btn: ['确定','取消'],
                }, function(){
                    if(_this.attr('url') !== undefined){
                        //获取URL
                        target = $(this).attr('url');
                    }else{
                        target = form.get(0).action;
                    }
                    fdata = form.serialize();//获取表单提交的数据
                    var load = layer.load(1, {shade: [0.1,'#fff']}); //0代表加载的风格，支持0-2
                    $.post(target,fdata).success(function(data){
                        layer.close(load);
                        tips(data,layer);
                    });
                });
            }else{
                fdata = form.serialize();//获取表单提交的数据
                var load = layer.load(1, {shade: [0.1,'#fff']}); //0代表加载的风格，支持0-2
                $.post(target,fdata).success(function(data){
                    layer.close(load);
                    tips(data,layer);
                });
            }
        }else{
            return false;
        }
    });
//ajax get请求
    $('.ajax-get').click(function(){
        var check = $(this).attr("target-check");
        var load = layer.load(1, {shade: [0.1,'#fff']}); //0代表加载的风格，支持0-2
        var target,target_msg;
        var _this = $(this);
        var target_msg = _this.attr('target-msg');
        if(target_msg !== undefined){
            //判断提交前的再次确定信息
            layer.confirm(target_msg, {
                btn: ['确定','取消'],
            }, function(){
                if(_this.attr('target-url') !== undefined){
                    //获取URL
                    target = _this.attr('target-url');
                }else{
                    //获取URL
                    target = _this.attr('url');
                }
                $.get(target).success(function(data){
                    layer.close(load);
                    tips(data,layer);
                });
            }, function(){
                layer.close(load);
            });
        }else{
            if(_this.attr('target-url') !== undefined){
                //获取URL
                target = _this.attr('target-url');
            }else{
                //获取URL
                target = _this.attr('url');
            }
            $.get(target).success(function(data){
                layer.close(load);
                tips(data,layer);
            });
        }
        return false;
    });
    /*
     提交判断回调是否跳转URL
     */
    function alt(data) {
        if(data.close == 'on'){
            if(data.refresh){
                parent.location.reload();
            }else if(data.url){
                parent.location.href = data.url;
            }
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index);
        }
        if(data.refresh){
            location.reload();
        }else if(data.url){
            location.href = data.url;
        }else if(data.fun){
            eval(data.fun);
        }
    }
    /*
     提交判断回调提示类型
     */
    function tips(data,lay) {
        if (data.status==1) {
            var ico = 1;
        }else{
            var ico = 2
        }
        if(data.tips == 1){
            layer.msg(data.msg);
            setTimeout(function(){
                alt(data);
            },1500);
        }else if(data.tips == 2){
            layer.alert(data.msg, {
                title:'提示',
                icon: ico,
                skin: 'layer-ext-moon',
                yes: function(index, layero){
                    layer.close(index);
                    alt(data);
                }
            });
        }else{
            alt(data);
        }
    }
    /*
     判断是否为空
     */
    function isnull(data) {
        var text = '';
        if(data[0].tagName == "INPUT"){
            text = $.trim(data.val());
        }else{
            text = $.trim(data.val());
        }
        if (text && typeof(text)!="undefined" && text!=0){
            return 1;
        }else{
            layer.tips('内容为空', data);
            return 0;
        }
    }
    /*
     验证判断
     */
    function verif(data) {
        var f = 0;
        data.each(function () {
            if($(this).hasClass("verif_null")){
                var s = isnull($(this));
                f += s;
            }
        });
    }
});
$(document).keyup(function(event){
    if(event.keyCode ==13){
        $(".13keyCode").trigger("click");
    }
});